<?php
	
	// breadcrumbs
	$this->breadcrumbs = array(
		'Posts' => array('index'),
		'Manage',
	);
	
?>

<div class="page-header">
    <?php echo CHtml::link('<i class="icon-plus icon-white"></i> Create post', array('post/create'), array('class' => 'btn btn-primary')) ?>
    <h1>Manage Posts</h1>
</div>

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'post-grid',
	'dataProvider' => $model->search(),
	'filter' => $model,
	'columns' => array(
		array(
			'name' => 'title',
			'type' => 'raw',
			//'value' => 'CHtml::link(CHtml::encode($data->title), $data->url)',
            'value' => 'CHtml::link(CHtml::encode($data->title), Yii::app()->baseUrl."/post/".$data->id)'
		),
		array(
			'name' => 'status',
			'value' => 'Lookup::item("PostStatus",$data->status)',
			'filter' => Lookup::items('PostStatus'),
		),
		array(
			'name' => 'create_time',
			'type' => 'datetime',
			'filter' => false,
		),
        array(
			'name' => 'update_time',
			'type' => 'datetime',
			'filter' => false,
		),
		array(
			'class' => 'CButtonColumn',
            'template'=>'{view} {update} {delete}',
            'viewButtonImageUrl' => Yii::app()->baseUrl.'/css/admin/gridview/view.png',
            'viewButtonUrl' => 'Yii::app()->baseUrl."/post/".$data->id',
            // Tooltip for view button
            'viewButtonOptions'=>array(
                'target' => '_blank',
                'rel'=>'tooltip',
                'data-original-title'=>'Details',
                'title'=>'',
            ),
            'updateButtonImageUrl' => Yii::app()->baseUrl.'/css/admin/gridview/update.png',
            // Tooltip for update button
            'updateButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Edit',
                'title'=>'',
            ),
            'deleteButtonImageUrl' => Yii::app()->baseUrl.'/css/admin/gridview/delete.png',
            // Tooltip for delete button
            'deleteButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Delete',
                'title'=>'',
            ),
            'buttons'=>array(
                'delete' => array(
                    'click'=>"function( e ){
                        e.preventDefault();
                        $('#modalUpdate .modal-body').children(':eq(0)').empty(); // Stop auto POST
                        $('#modalUpdate .modal-header h3').html('Delete Confirm');
                        updateModal($(this).attr('href'));
                        $('#modalUpdate').modal('show');
                    }",
                ),
            ),
		),
	),
)); ?>

<!-- Modal for Update -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id'=>'modalUpdate',
    'options'=>array(
        'title'=>'Update Category',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<?php
$updateJS = CHtml::ajax(array(
    'url'=>"js:url",
    'data'=>"js:form.serialize() + action",
    'type'=>'post',
    'dataType'=>'json',
    'success'=>"function(data)
    {
        if(data.status == 'failure')
        {
            $('#modalUpdate div.modal-body').html(data.content);
            $('#modalUpdate div.modal-body form input[type=submit]')
                .die() // Stop from re-binding event handlers
                .live('click', function(e){ // Send clicked button value
                e.preventDefault();
                updateModal(false, $(this).attr('name'));
            });
        }
        else
        {
            $('#modalUpdate div.modal-body').html(data.content);
            if(data.status == 'success') // Update all grid views on success
            {
                $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                    $.fn.yiiGridView.update($(this).attr('id'));
                });
            }
            $('#modalUpdate').modal('hide');
            $('#modalUpdate .modal-body').children(':eq(0)').empty();
        }
    }"
)); ?>

<?php
Yii::app()->clientScript->registerScript('updateModal', "function updateModal(url, act)
    {
        var action = '';
        var form = $('#modalUpdate div.modal-body form');
        if(url == false)
        {
            action = '&action=' + act;
            url = form.attr('action');
        }
        {$updateJS}
    }" 
); ?>